#ifndef ConstraintDetails_H
#define ConstraintDetails_H

#include "Details.h"
#include "ConstraintItem.h"
#include "ConstraintModel.h"
#include "CompareModeDelegate.h"

namespace Xps
{
	class ConstraintDetails : public Details
	{
		Q_OBJECT 

		QPixmap swap_img;
		QPixmap attr_img;
		QPixmap del_img;
		QPushButton* del;	
		QPushButton* swap;

	public:
		QTableView* table;
		QPushButton* add;

		QHBoxLayout* buttonpanel;
		ConstraintModel* cm;
		XpsAssets::AssetManager* am;
		ConstraintItem* ci;
		QItemSelectionModel* selectionmodel;
		
		ConstraintDetails(XpsAssets::AssetManager* am) : Details("Constraint Item", ":/rs/images/constraint.png")
		{
			this->am = am;
			ci = NULL;

			buttonpanel = new QHBoxLayout();		
			cm = new ConstraintModel(am);

			swap_img = QPixmap(":/rs/images/swap.png");
			attr_img = QPixmap(":/rs/images/attribute.png");
			del_img = QPixmap(":/rs/images/delete.png");

			table = new QTableView();
			table->horizontalHeader()->hide();
			table->setShowGrid(false);
			table->setAlternatingRowColors(true);
			table->setItemDelegateForColumn(0, new CompareModeDelegate(this));
			table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
			table->setEditTriggers(QAbstractItemView::AllEditTriggers);
			table->setSelectionBehavior(QAbstractItemView::SelectRows);
			
			swap = new QPushButton();
			swap->setIcon(QIcon(swap_img));
			swap->setFixedWidth(30);
			
			add = new QPushButton();
			add->setIcon(QIcon(attr_img));
			add->setFixedWidth(30);

			del = new QPushButton();
			del->setIcon(QIcon(del_img));
			del->setFixedWidth(30);
			del->setDisabled(true);

			buttonpanel->addStretch(0);
			buttonpanel->addWidget(swap);
			buttonpanel->addWidget(del);
			buttonpanel->addWidget(add);
			
			content_layout->addWidget(table);
			content_layout->addLayout(buttonpanel);

			table->setModel(cm);
			table->setSelectionMode(QAbstractItemView::SingleSelection);
			selectionmodel = table->selectionModel();

			connect(swap, SIGNAL(clicked()), this, SLOT(swapNodes()));
			connect(selectionmodel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(selectionChanged(QItemSelection,QItemSelection)));			
			connect(del, SIGNAL(clicked()), this, SLOT(delClicked()));
		}

		void setItem(Item* item)
		{
			if (item->getType() != XpsAssets::TreeNode::CONSTRAINT)
				return;

			ci = (ConstraintItem*) item;
			
			cm->setDataSource(ci->attributes());
		}

	public slots:
		void addAttribute(int id)
		{
			cm->addAttribute(id);
		}
		void remAttribute(int id)
		{
			cm->remAttribute(id);
		}

		void swapNodes()
		{
			ci->swap();
		}

		void selectionChanged(const QItemSelection & selected,const QItemSelection & deselected)
		{
			QList<QModelIndex> selection = selectionmodel->selectedIndexes();
			
			if (selection.count() == 0)
			{
				del->setDisabled(true);
				return;
			}
			else 
				del->setEnabled(true);
		}

		void delClicked()
		{
			QList<QModelIndex> selection = selectionmodel->selectedIndexes();
			QList<int> delids;

			for (int i = 0; i < (selection.count()/table->model()->columnCount()); i++)
			{
				int id = table->model()->data(selection.at(i), Qt::UserRole).toInt();
				
				delids.append(id);
			}

			while (!delids.isEmpty())
			{
				remAttribute(delids.takeAt(0));
			}
		}
	};
	
}
#endif